/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.view.controller;
 
import cz.cvut.fel.taskpro.business.imanager.IAssignmentBusiness;
import cz.cvut.fel.taskpro.business.imanager.ISubjectBusiness;
import cz.cvut.fel.taskpro.business.imanager.IVersionBusiness;
import cz.cvut.fel.taskpro.business.support.BusinessRuleException;
import cz.cvut.fel.taskpro.data.entity.Assignment;
import cz.cvut.fel.taskpro.data.entity.Assignment.Type;
import cz.cvut.fel.taskpro.data.entity.AssignmentSubmit;
import cz.cvut.fel.taskpro.data.entity.AssignmentSubmit.State;
import cz.cvut.fel.taskpro.data.entity.Subject;
import cz.cvut.fel.taskpro.data.entity.VersionFile;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Tom
 */
@ManagedBean(name = "studentAsSubmitBean")
@ViewScoped
public class StudentSubmitTaskController extends AbstractFileController implements Serializable {

    @EJB
    private ISubjectBusiness subjectB;
    @EJB
    private IAssignmentBusiness assignmentB;
    @EJB
    private IVersionBusiness versionB;
    private List<AssignmentSubmit> submitAssingment;
    private List<Subject> subjects;
    private List<AssignmentSubmit.State> states;
    private List<Assignment.Type> typeOfAssignment;
    private AssignmentSubmit.State[] selectedStates;
    private Assignment.Type[] selectedTypes;
    private AssignmentSubmit selectedAssignmentSubmit;
    private List<VersionFile> versions;
    private List<String> selectedSubject;
    private Map<String,Subject> subjectMap;
 
    @PostConstruct
    public void init() {
        recognizeLogin();
        subjectMap=new HashMap<String, Subject>();
        selectedSubject=new ArrayList<String>();
        states = new ArrayList<AssignmentSubmit.State>();
        states.add(AssignmentSubmit.State.ACCEPTED);
        states.add(AssignmentSubmit.State.REJECTED);
        states.add(AssignmentSubmit.State.SUBMITTED);
        typeOfAssignment = new ArrayList<Assignment.Type>();
        typeOfAssignment.add(Assignment.Type.TEAM);
        typeOfAssignment.add(Assignment.Type.STUDENT);
        try {
            subjects = subjectB.getStudentsSubjects(loggedStudent);
            submitAssingment = assignmentB.getAssignmentSubmitsByContext(loggedStudent, subjects, states, typeOfAssignment);
            Iterator subIt=subjects.iterator();
            while(subIt.hasNext()){
                Subject currSub=(Subject) subIt.next();
                subjectMap.put(currSub.getCode(), currSub);
                selectedSubject.add(currSub.getCode());
            }
            selectedStates = states.toArray(new AssignmentSubmit.State[states.size()]);
            selectedTypes = typeOfAssignment.toArray(new Assignment.Type[typeOfAssignment.size()]);       
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }

    public void applyFilter() {
        try {
            List<Subject> subjectToFilter=new ArrayList<Subject>();
            Iterator itSub=selectedSubject.iterator();
            while(itSub.hasNext()){
                String code=(String) itSub.next();
                subjectToFilter.add(subjectMap.get(code));
            }
            submitAssingment = assignmentB.getAssignmentSubmitsByContext(loggedStudent, subjectToFilter, Arrays.asList(selectedStates), Arrays.asList(selectedTypes));
            printSuccMessage("Filter: ", "filter has been successfully set up.");
        } catch (BusinessRuleException ex) {
            printErrMessage(ex.getExceptionCode(), ex.getErrorMessage());
        }
    }
    public void loadVersions() {
        versions = versionB.getAllVersionFileByAssingment(selectedAssignmentSubmit);
    }

    public AssignmentSubmit getSelectedAssignmentSubmit() {
        return selectedAssignmentSubmit;
    }

    public void setSelectedAssignmentSubmit(AssignmentSubmit selectedAssignmentSubmit) {
        this.selectedAssignmentSubmit = selectedAssignmentSubmit;
    }

    public State[] getSelectedStates() {
        return selectedStates;
    }

    public void setSelectedStates(State[] selectedStates) {
        this.selectedStates = selectedStates;
    }

    public List<String> getSelectedSubject() {
        return selectedSubject;
    }

    public void setSelectedSubject(List<String> selectedSubject) {
        this.selectedSubject = selectedSubject;
    }

    public Type[] getSelectedTypes() {
        return selectedTypes;
    }

    public void setSelectedTypes(Type[] selectedTypes) {
        this.selectedTypes = selectedTypes;
    }

    public List<State> getStates() {
        return states;
    }

    public void setStates(List<State> states) {
        this.states = states;
    }

    public List<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<Subject> subjects) {
        this.subjects = subjects;
    }

    public List<AssignmentSubmit> getSubmitAssingment() {
        return submitAssingment;
    }

    public void setSubmitAssingment(List<AssignmentSubmit> submitAssingment) {
        this.submitAssingment = submitAssingment;
    }

    public List<Type> getTypeOfAssignment() {
        return typeOfAssignment;
    }

    public void setTypeOfAssignment(List<Type> typeOfAssignment) {
        this.typeOfAssignment = typeOfAssignment;
    }

    public List<VersionFile> getVersions() {
        return versions;
    }

    public void setVersions(List<VersionFile> versions) {
        this.versions = versions;
    }
}
